﻿Imports ISNet.WebUI.WebTreeView
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class R04
    Inherits System.Web.UI.Page

    Public ScriptText As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("R04")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Label1.Text = "ระบบจัดทำงบประมาณ >> รายงาน >> รายงานราคามาตรฐาน"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        GenerateValues()
        If IsPostBack = False Then
            Try
                Dim adap As New DBdataTableAdapters.AppConfigsTableAdapter
                Dim dt As New DBdata.AppConfigsDataTable
                dt = adap.GetData
                For Each item As DBdata.AppConfigsRow In dt
                    Select Case item.Name
                        Case "ACTIVE_PLAN_YEAR"
                            PlanYearsTxt.Text = item.Value
                    End Select
                Next
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
        End If

        Dim adapter As New Data_ReportTableAdapters.ActivityTypesTableAdapter
        Dim data As New Data_Report.ActivityTypesDataTable

        data = adapter.GetDataByPlanYear(Val(PlanYearsTxt.Text))

        ms8.DataValueField = "ActivityTypeCode"
        ms8.DataTextField = "Description"
        ms8.DataSource = data
        ms8.DataBind()

    End Sub


    Sub GenerateValues()

        If hdf1.Value.Length = 0 Then
            Session("Search") = "false"
        End If
        'MsgBox(hdf1.Value)

        Try
            Dim Numx As Array = hdf1.Value.Split(",")


            For i = 0 To Numx.Length - 1
                ms8.Items.FindByValue(Numx(i).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try

    End Sub

    Protected Sub toReport_Click(sender As Object, e As ImageClickEventArgs) Handles toReport.Click
        GenerateValues()


        If Val(PlanYearsTxt.Text) = 0 Then
            ScriptText = "<script language='javascript'>alert('กรุณาระบุปีงบประมาณ');</script>"
            Exit Sub
        End If


        If WebCombo1.Value <> "" And WebCombo1.Value = "" Then
            ScriptText = "<script language='javascript'>alert('กรุณาเลือก ครุภัณฑ์ ถึง');</script>"
            Exit Sub
        End If

        Session.Remove("year")
        Session.Add("year", PlanYearsTxt.Text)

        Session.Remove("Assetgroup")
        Session.Add("Assetgroup", AssetGroupCombo.Value)


        Session.Remove("ActivityTCFrom")
        Session.Add("ActivityTCFrom", WebCombo1.Value)

        Session.Remove("ActivityTCTo")
        Session.Add("ActivityTCTo", WebCombo2.Value)

        'Dim sql As String
        'If hdf1.Value.ToString <> "" Then
        '    sql = "("
        '    Dim Numx As Array = hdf1.Value.Split(",")
        '    For i = 0 To Numx.Length - 1
        '        If Val(Numx(i)) <> 0 Then
        '            If i <> 0 Then
        '                sql += ","
        '            End If
        '            sql += Numx(i).ToString
        '        End If

        '    Next

        '    sql += ") "

        'End If


        Session.Remove("ActivityTCSelect")
        Session.Add("ActivityTCSelect", hdf1.Value)



        ScriptText = "<script language='javascript'>openpop();</script>"

    End Sub

    Protected Sub AssetGroup_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles AssetGroupCombo.InitializeDataSource
        Dim dt As New DataTable
        dt.Columns.Add("AssetGroup")
        dt.Columns.Add("Description")

        dt.Rows.Add(1, "ครุภัณฑ์สำนักงาน")
        dt.Rows.Add(2, "ครุภัณฑ์คอมพิวเตอร์")
        dt.Rows.Add(3, "ครุภัณฑ์ขนส่ง")

        e.DataSource = dt

    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adapter As New Data_ReportTableAdapters.ActivityTypesTableAdapter
        Dim data As New Data_Report.ActivityTypesDataTable

        data = adapter.GetDataByPlanYear(Val(Session("year")))
        ' data.Rows.Add("0", "ทุกรายการ", "1", "0")
        data.DefaultView.Sort = "ActivityTypeCode"
        e.DataSource = data
    End Sub



End Class
